package ls.vuecp.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.KeyStroke;

import tufts.vue.VueResources;

import ls.vuecp.log.Logger;

public class VueAction extends AbstractAction
{
	private static Logger Log = Logger.getLogger();
	public static final boolean EnableSmallIconsForMenus = false;
	public static final String LARGE_ICON = "vue.largeIcon";
	private static final String CHECKBOX_LIST = "vue.checkBoxList";
	
	public static List<VueAction> allActionList = new ArrayList();
	public static List<VueAction> getAllActions() { 
		return Collections.unmodifiableList(allActionList); }
	
	private static boolean allIgnored = false;
	protected static long currentActionTime;
	
    private final String permanentName;
	private final KeyStroke keyStroke;
	public KeyStroke getKeyStroke() {
        return keyStroke;
      //return (KeyStroke) getValue(ACCELERATOR_KEY);
    }
	
	
	public VueAction(String name, String shortDescription, KeyStroke keyStroke, Icon icon)
    {
        super(name);
        
        this.keyStroke = keyStroke;
        this.permanentName = name;

        if (shortDescription != null)
            putValue(SHORT_DESCRIPTION, shortDescription);
        else
            putValue(SHORT_DESCRIPTION, name);
        if (keyStroke != null)
            putValue(ACCELERATOR_KEY, keyStroke);

        setSmallIcon(icon);
        allActionList.add(this);
    }
    public VueAction(String name, KeyStroke keyStroke, String iconSpec) {
        this(name, null, keyStroke, null);
        setIcon(iconSpec);
        //setSmallIcon(icon);
    }
    public VueAction(String name, KeyStroke keyStroke) {
        this(name, null, keyStroke, null);
    }
    public VueAction(String name) {
        this(name, null, null, null);
    }
    public VueAction(String name, Icon icon) {
        this(name, null, null, icon);
    }
	
    
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		Log.info("VueAction: "+this.permanentName);
	}
	
	
	private void setIcon(String iconSpec) {
        Icon icon = null;
        if (iconSpec.startsWith(":")) {
            if (EnableSmallIconsForMenus) {
                icon = VueResources.getImageIconResource("/toolbarButtonGraphics/" + iconSpec.substring(1) + "16.gif");
                setSmallIcon(icon);
            }
            icon = VueResources.getImageIconResource("/toolbarButtonGraphics/" + iconSpec.substring(1) + "24.gif");
            putValue(LARGE_ICON, icon);
        } else {
            if (EnableSmallIconsForMenus) {
                icon = VueResources.getImageIconResource(iconSpec);
                setSmallIcon(icon);
            }
        }
    }
    private void setSmallIcon(Icon icon) {
        if (EnableSmallIconsForMenus) {
            if (icon != null) {
                //if (icon != tufts.vue.gui.GUI.NO_ICON)
                    putValue(SMALL_ICON, icon);
            }
        }
    }
    
    
    
	private static final Boolean IS_NOT_A_TOGGLER = new Boolean(false);
    /**
     * if this is overridden to return a varying result, any AbstractButton watchers
     * of this action (added via trackToggler) will have their setSelected method
     * called with it's value.
     */
    public Boolean getToggleState() {
        // returning a fixed boolean instance is a clever way of knowing if
        // this has been overridden
        return IS_NOT_A_TOGGLER;
    }
    public boolean isToggler() {
        return getToggleState() != IS_NOT_A_TOGGLER;
    }
    /** add a button that supports toggle state (e.g., a
    /** JCheckBoxMenuItem) to be updated if this action represents the
    /** toogle of a boolean the checkbox should stay synced with */
    
    public void trackToggler(AbstractButton toggler) {

        Collection list = (Collection) getValue(CHECKBOX_LIST);
        if (list == null)
            putValue(CHECKBOX_LIST, list = new java.util.ArrayList());
        
        list.add(toggler);
        toggler.setSelected(getToggleState());
    }
}
